/*
 * Copyright (c) 2009, DanikX Corporation. All Rights Reserved.
 */

package kz.home.gui;

import kz.home.db.DBUtils;
import kz.home.utils.Utils;

import javax.microedition.lcdui.*;
import java.util.Vector;
import java.util.Hashtable;

/**
 *
 * Created by IntelliJ IDEA.
 * User: danik
 * Date: 08.07.2009
 * Time: 2:20:30
 * To change this template use File | Settings | File Templates.
 */
public class CitiesForm extends BaseForm {
    protected Vector cities;
    protected Command cmdSelect;
    protected Vector event;


    public CitiesForm(BaseForm mainScreen, Display display, Hashtable table, Vector event) {
        super("Выберите город", mainScreen, display, table);
        this.event = event;
        cities = getVector("" + event.elementAt(4));
        initMap(DBUtils.CITIES);
        initView();
        initCmd();
    }

    public String getParamName() {
        return "cit";
    }

    private void initCmd() {
        cmdSelect = new Command("Выбрать", Command.BACK, 2);
        addCommand(cmdSelect);
    }

    private void initView() {
        for (int i = 0; i < cities.size(); i++) {
            append("" + map.get(cities.elementAt(i)));
        }
    }

    protected void addToMap(Hashtable map, Vector dataRow) {
        map.put(dataRow.elementAt(0),//id
                dataRow.elementAt(1) // город
        );
    }

    protected Vector getVector(String arr) {
        return Utils.getTokens(arr.substring(1, arr.length() - 1), ",");
    }

    protected void initSend() {

    }

    public void commandAction(Command command, Displayable displayable) {
        super.commandAction(command, displayable);

        if (command == cmdSelect) {
            String cityID = "" + cities.elementAt(getSelectedIndex());
            setValue(cityID);
            ChooseForm form = new ChooseForm(this, display, request, cityID, event);
            display.setCurrent(form);
        }
    }

    protected String getParam() {
        return "cit";
    }
}
